﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using Catering_Model;
using System.Text;

namespace Catering.Catering_Page.JieGouJianKong
{
    public partial class XiaohaoChar : System.Web.UI.Page
    {

        #region 属性

        private static int[] RawId;//食品大类ID
        View_Monitor vm = new View_Monitor();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void lbtnTyleChar_Click(object sender, EventArgs e)
        {
            string areaId = UCDDLToolWithYearOfMonth1.GetDDL_Area();         //作业区ID
            DateTime year_current = UCDDLToolWithYearOfMonth1.GetDateTime(); //当前年月
            string timeType = UCDDLToolWithYearOfMonth1.GetTimeType();       //对比方式（按年、按月）
            string[] years = UCSearchToolByZW1.GetYears();                   //对比的年份
            RawId = UCSearchToolByZW1.GetRaw();
            string classId = UCDDLToolWithYearOfMonth1.GetDDL_Class();        //班组ID
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();

            //判断当前年月
            if (year_current.Year == 1949) return;
            //判断对比方式
            if (timeType == "Year")
            {
                string[] lastYears = null;
                if (years != null && years.Contains(year_current.Year.ToString()))
                {
                    lastYears = years;
                }
                else
                {
                    lastYears = new string[(years == null ? 1 : (years.Count() + 1))];
                    lastYears[0] = year_current.Year.ToString();
                    if (years != null)
                    {
                        for (int i = 0; i < years.Count(); i++)
                        {
                            if (years[i].Equals(year_current.Year.ToString()))
                            {
                                continue;
                            }
                            lastYears[i + 1] = years[i];
                        }
                    }
                }
                GetXiaohaoCharByYear(pid,lastYears, classId, areaId);
            }
            else
            {
                GetXiaohaoCharByMonth(year_current, classId, areaId);
            }
        }

        protected void GetXiaohaoCharByYear(string strarealist, string[] lastYears, string classId, string areaId)
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetXiaohaoChar(pid,areaId, lastYears, RawId, classId);
            BiaoList bl = new BiaoList();

            #region 标题

            string Title = "";
            for (int i = 0; i < lastYears.Count(); i++)
            {
                if (i == 0)
                {
                    Title += lastYears[i];
                }
                else if (!string.IsNullOrEmpty(lastYears[i]))
                {
                    Title += "、" + lastYears[i];
                }
            }
            Title += "年";
            if (!string.IsNullOrEmpty(areaId) && !areaId.Equals("-10"))
            {
                Catering_Model.OperationArea AreaInfo = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(areaId));
                Title += AreaInfo.AreaName;
                if (!string.IsNullOrEmpty(classId) && !classId.Equals("-10"))
                {
                    Catering_Model.ClassInfo ClassInfo = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(classId));
                    Title += ClassInfo.CName + "班组";
                }
                else
                {
                    Title += "各班组";
                }
            }
            else
            {
                Title += "所有作业区";
            }
            Title += "分类统计";

            #endregion

            #region 数据

            if (lastYears.Length > 1)
            {
                bl.biaolist = new Biao[lastYears.Length * RawId.Length];
                int num = 0;
                for (int i = 0; i < RawId.Length; i++)
                {
                    //禽肉类：3、鱼虾类：4、蔬菜类：6、水果类：7、谷类薯类及杂豆：8、油：10、盐：11
                    string rawName = GetRawName(RawId[i]);
                    for (int j = 0; j < lastYears.Length; j++)
                    {
                        bl.biaolist[num] = new Biao();
                        bl.biaolist[num].name = lastYears[j] + "年" + rawName;
                        bl.biaolist[num].data = new decimal[12];
                        num++;
                    }
                }
            }
            else
            {
                bl.biaolist = new Biao[RawId.Length];
                for (int i = 0; i < RawId.Length; i++)
                {
                    //禽肉类：3、鱼虾类：4、蔬菜类：6、水果类：7、谷类薯类及杂豆：8、油：10、盐：11
                    string rawName = GetRawName(RawId[i]);
                    bl.biaolist[i] = new Biao();
                    bl.biaolist[i].name = rawName;
                    bl.biaolist[i].data = new decimal[12];
                }
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string foodtype = dt.Rows[i]["FoodType"].ToString();
                int month = Convert.ToInt32(dt.Rows[i]["Month"]);
                int year = Convert.ToInt32(dt.Rows[i]["NumYear"]);
                decimal number = Convert.ToDecimal(dt.Rows[i]["Numbers"]);
                for (int j = 0; j < bl.biaolist.Length; j++)
                {
                    if (lastYears.Length == 1)
                    {
                        if (foodtype != bl.biaolist[j].name) continue;
                    }
                    else
                    {
                        if ((year + "年" + foodtype) != bl.biaolist[j].name) continue;

                    }
                    bl.biaolist[j].data[(month - 1)] = bl.biaolist[j].data[(month - 1)] + number;
                }
            }

            string str = "";
            if (bl != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                foreach (Biao biao in bl.biaolist)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + biao.name + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (decimal num in biao.data)
                    {
                        StrData += num + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    Sresult.Append("},");
                }
                str = Sresult.ToString();
            }
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            #endregion

            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "',2," + str + ");", true);
        }

        public string GetRawName(int type)
        {
            string result = "";
            switch (type)
            {
                case 3:
                    result = "禽肉类";
                    break;
                case 4:
                    result = "鱼虾类";
                    break;
                case 6:
                    result = "蔬菜类";
                    break;
                case 7:
                    result = "水果类";
                    break;
                case 8:
                    result = "谷类薯类及杂豆";
                    break;
                case 10:
                    result = "油";
                    break;
                case 11:
                    result = "盐";
                    break;
            }
            return result;
        }

        protected void GetXiaohaoCharByMonth(DateTime year_current, string classId, string areaId)
        {
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetXiaohaoCharByMonth(pid,areaId, year_current, RawId, classId);
            BiaoList bl = new BiaoList();

            #region 标题

            string Title = year_current.Year + "年" + year_current.Month + "月";
            if (!string.IsNullOrEmpty(areaId) && !areaId.Equals("-10"))
            {
                Catering_Model.OperationArea AreaInfo = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(areaId));
                Title += AreaInfo.AreaName;
                if (!string.IsNullOrEmpty(classId) && !classId.Equals("-10"))
                {
                    Catering_Model.ClassInfo ClassInfo = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(classId));
                    Title += ClassInfo.CName + "班组";
                }
                else
                {
                    Title += "各班组";
                }
            }
            else
            {
                Title += "所有作业区";
            }
            Title += "消耗分类统计";

            #endregion

            bl.biaolist = new Biao[RawId.Length];
            for (int i = 0; i < RawId.Length; i++)
            {
                //禽肉类：3、鱼虾类：4、蔬菜类：6、水果类：7、谷类薯类及杂豆：8、油：10、盐：11
                string rawName = GetRawName(RawId[i]);
                bl.biaolist[i] = new Biao();
                bl.biaolist[i].name = rawName;
                bl.biaolist[i].data = new decimal[31];
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string foodtype = dt.Rows[i]["FoodType"].ToString();
                int day = Convert.ToInt32(dt.Rows[i]["Day"]);
                decimal number = Convert.ToDecimal(dt.Rows[i]["Numbers"]);
                for (int j = 0; j < bl.biaolist.Length; j++)
                {
                    if (foodtype != bl.biaolist[j].name) continue;
                    bl.biaolist[j].data[(day - 1)] = number;
                }
            }

            string str = "";
            if (bl != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                foreach (Biao biao in bl.biaolist)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + biao.name + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (decimal num in biao.data)
                    {
                        StrData += num + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    Sresult.Append("},");
                }
                str = Sresult.ToString();
            }
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "',1," + str + ");", true);
        }
    }
}